package com.example.service.impl;

import com.example.common.AppResult;
import com.example.common.ResultCode;
import com.example.dao.ArticleMapper;
import com.example.dao.ArticleReplyMapper;
import com.example.exception.ApplicationException;
import com.example.model.Article;
import com.example.model.ArticleReply;
import com.example.service.IArticleReplyService;
import com.example.service.IArticleService;
import com.example.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-10-16
 * Time: 19:54
 */
@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private IArticleService articleService;

    /**
     * 创建帖子回复
     * @param articleReply 帖子回复信息
     */
    @Override
    @Transactional //事务管理
    public void createArticleReply(ArticleReply articleReply) {
        //1.非空校验,帖子回复内容不能为空;同时帖子回复需要关联帖子和用户
        if(articleReply == null || articleReply.getPostUserId() == null || articleReply.getArticleId() == null
        || StringUtils.isEmpty(articleReply.getContent())){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.设置帖子回复对象默认值
        articleReply.setState((byte)0);
        articleReply.setDeleteState((byte)0);
        articleReply.setLikeCount(0);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        //3.写入数据库
        int insertCount = articleReplyMapper.insertSelective(articleReply);
        if(insertCount != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //4.更新帖子回复数
        articleService.addOneArticleReplyCountById(articleReply.getArticleId());
        //5.打印日志
        log.info("创建帖子回复成功,帖子回复id="+articleReply.getId()+",帖子id="+articleReply.getArticleId()
        +",用户id="+articleReply.getPostUserId());

    }

    /**
     * 根据帖子id查询帖子回复列表
     * @param articleId 帖子id
     * @return 帖子回复列表
     */
    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //1.非空校验
        if(articleId == null || articleId <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.查询数据库
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        return result;
    }
}
